/**
 * mime.js
 * @author Ulyan Sobin
 * @description MIME type map module
 */

"use strict";

const MIMEList = {
	/* Text format */
	"txt": "text/plain",
	"html": "text/html",
	"htm": "text/html",
	"css": "text/css",
	/* Image format */
	"jpg": "image/jpeg",
	"jpeg": "image/jpeg",
	"png": "image/png",
	"gif": "image/gif",
	"webp": "image/webp",
	"bmp": "image/bmp",
	"icon": "image/x-icon",
	"svg": "image/svg+xml",
	"cod": "image/cis-cod",
	"ief": "image/ief",
	"jpe": "image/jpeg",
	"jfif": "image/pipeg",
	"tif": "image/tiff",
	"tiff": "image/tiff",
	"ras": "image/x-cmu-raster",
	"cmx": "image/x-cmx",
	"ico": "image/x-icon",
	"pnm": "image/x-portable-anymap",
	"pbm": "image/x-portable-bitmap",
	"pgm": "image/x-portable-graymap",
	"ppm": "image/x-portable-pixmap",
	"rgb": "image/x-rgb",
	"xbm": "image/x-xbitmap",
	"xpm": "image/x-xpixmap",
	"xwd": "image/x-xwindowdump",
	/* Video format */
	"webm": "video/webm",
	"mp4": "video/mp4",
	"wmv": "video/wmv",
	"ogg": "video/ogg",
	"mpg": "video/mpeg",
	"mpeg": "video/mpeg",
	"mp2": "video/mpeg",
	"mpa": "video/mpeg",
	"mpe": "video/mpeg",
	"mpv2": "video/mpeg",
	"mov": "video/quicktime",
	"qt": "video/quicktime",
	"lsf": "video/x-la-asf",
	"lsx": "video/x-la-asf",
	"asf": "video/x-ms-asf",
	"asr": "video/x-ms-asf",
	"asx": "video/x-ms-asf",
	"avi": "video/x-msvideo",
	"movie": "video/x-sgi-movie",
	/* Audio format */
	"mp3": "audio/mpeg",
	"mid": "audio/midi",
	"rmi": "audio/midi",
	"cmf": "audio/midi",
	"wma": "audio/wma",
	"aac": "audio/aac",
	"au": "audio/basic",
	"snd": "audio/basic",
	"aif": "audio/x-aiff",
	"aifc": "audio/x-aiff",
	"aiff": "audio/x-aiff",
	"m3u": "audio/x-mpegurl",
	"ra": "audio/x-pn-realaudio",
	"ram": "audio/x-pn-realaudio",
	"wav": "audio/x-wav",
	/* Other format */
	"js": "application/javascript",
	"xml": "application/xml",
	"evy": "application/envoy",
	"fif": "application/fractals",
	"spl": "application/futuresplash",
	"hta": "application/hta",
	"acx": "application/internet-property-stream",
	"hqx": "application/mac-binhex40",
	"doc": "application/msword",
	"dot": "application/msword",
	"oda": "application/oda",
	"axs": "application/olescript",
	"pdf": "application/pdf",
	"prf": "application/pics-rules",
	"p10": "application/pkcs10",
	"crl": "application/pkix-crl",
	"ai": "application/postscript",
	"eps": "application/postscript",
	"ps": "application/postscript",
	"rtf": "application/rtf",
	"setpay": "application/set-payment-initiation",
	"setreg": "application/set-registration-initiation",
	"xla": "application/vnd.ms-excel",
	"xlc": "application/vnd.ms-excel",
	"xlm": "application/vnd.ms-excel",
	"xls": "application/vnd.ms-excel",
	"xlt": "application/vnd.ms-excel",
	"xlw": "application/vnd.ms-excel",
	"sst": "application/vnd.ms-pkicertstore",
	"cat": "application/vnd.ms-pkiseccat",
	"stl": "application/vnd.ms-pkistl",
	"pot": "application/vnd.ms-powerpoint",
	"pps": "application/vnd.ms-powerpoint",
	"ppt": "application/vnd.ms-powerpoint",
	"mpp": "application/vnd.ms-project",
	"wcm": "application/vnd.ms-works",
	"wdb": "application/vnd.ms-works",
	"wks": "application/vnd.ms-works",
	"wps": "application/vnd.ms-works",
	"hlp": "application/winhlp",
	"bcpio": "application/x-bcpio",
	"cdf": "application/x-cdf",
	"z": "application/x-compress",
	"tgz": "application/x-compressed",
	"cpio": "application/x-cpio",
	"csh": "application/x-csh",
	"dcr": "application/x-director",
	"dir": "application/x-director",
	"dxr": "application/x-director",
	"dvi": "application/x-dvi",
	"gtar": "application/x-gtar",
	"gz": "application/x-gzip",
	"hdf": "application/x-hdf",
	"ins": "application/x-internet-signup",
	"isp": "application/x-internet-signup",
	"iii": "application/x-iphone",
	"latex": "application/x-latex",
	"mdb": "application/x-msaccess",
	"crd": "application/x-mscardfile",
	"clp": "application/x-msclip",
	"dll": "application/x-msdownload",
	"m13": "application/x-msmediaview",
	"m14": "application/x-msmediaview",
	"mvb": "application/x-msmediaview",
	"wmf": "application/x-msmetafile",
	"mny": "application/x-msmoney",
	"pub": "application/x-mspublisher",
	"scd": "application/x-msschedule",
	"trm": "application/x-msterminal",
	"wri": "application/x-mswrite",
	"pma": "application/x-perfmon",
	"pmc": "application/x-perfmon",
	"pml": "application/x-perfmon",
	"pmr": "application/x-perfmon",
	"pmw": "application/x-perfmon",
	"p12": "application/x-pkcs12",
	"pfx": "application/x-pkcs12",
	"p7b": "application/x-pkcs7-certificates",
	"spc": "application/x-pkcs7-certificates",
	"p7r": "application/x-pkcs7-certreqresp",
	"p7c": "application/x-pkcs7-mime",
	"p7m": "application/x-pkcs7-mime",
	"p7s": "application/x-pkcs7-signature",
	"sh": "application/x-sh",
	"shar": "application/x-shar",
	"swf": "application/x-shockwave-flash",
	"sit": "application/x-stuffit",
	"sv4cpio": "application/x-sv4cpio",
	"sv4crc": "application/x-sv4crc",
	"tar": "application/x-tar",
	"tcl": "application/x-tcl",
	"tex": "application/x-tex",
	"texi": "application/x-texinfo",
	"texinfo": "application/x-texinfo",
	"roff": "application/x-troff",
	"t": "application/x-troff",
	"tr": "application/x-troff",
	"man": "application/x-troff-man",
	"me": "application/x-troff-me",
	"ms": "application/x-troff-ms",
	"ustar": "application/x-ustar",
	"src": "application/x-wais-source",
	"cer": "application/x-x509-ca-cert",
	"crt": "application/x-x509-ca-cert",
	"der": "application/x-x509-ca-cert",
	"pko": "application/ynd.ms-pkipko",
	"zip": "application/zip",
};

/**
 * Get the MIME type by extend file name
 * @param {string} extname
 * @returns {string}
 */
function getType(extname) {
	let en = extname.trim().toLowerCase()
	let R = MIMEList[en];
	if (R != undefined) return R;
	else return "application/octet-stream";
}

/**
 * Get MIME type by file path
 * @param {string} path
 * @returns {string}
 */
function mimeOf(path) {
	let dv1 = path.split(/[\/|\\]/);
	let filename = dv1[dv1.length - 1];
	let dv2 = filename.split('.');
	if (dv2.length > 1)
		return getType(dv2[dv2.length - 1]);
	else
		return "application/octet-stream";
}

module.exports.getType = getType;
module.exports.mimeOf = mimeOf;